package com.qfhd.soft.cjzcore.UI;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qfhd.soft.cjzcore.CJZManager;
import com.qfhd.soft.cjzcore.Core.CJCore;
import com.qfhd.soft.cjzcore.Handle.CJHandle;
import com.qfhd.soft.cjzcore.Handle.CJHandleObject;
import com.qfhd.soft.cjzcore.Handle.CJHanlderType;
import com.qfhd.soft.cjzcore.Handle.CJHanlderWhatType;
import com.qfhd.soft.cjzcore.UI.Dialog.CJDialog;
import com.qfhd.soft.cjzcore.Core.CJDpSpDxTransform;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Core.CJStorage;
import com.qfhd.soft.cjzcore.Core.V2BlurUtils;
import com.qfhd.soft.cjzcore.Moudle.CJGGKHelper;
import com.qfhd.soft.cjzcore.R;
import com.qfhd.soft.cjzcore.TaskCenter.CJAdCenter;
import com.qfhd.soft.cjzcore.TaskCenter.CSJ.CJCSJ_Navi;
import com.qfhd.soft.cjzcore.UI.Base.BaseActivity;
import com.qfhd.soft.cjzcore.UI.Custom.CJNaviBar;
import com.qfhd.soft.cjzcore.UI.Dialog.CJSignDialog;
import com.qfhd.soft.cjzcore.UI.Custom.ScratchView;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import static com.qfhd.soft.cjzcore.Handle.CJHanlderWhatType.LoadFinish;
import static com.qfhd.soft.cjzcore.Handle.CJHanlderWhatType.ShowDialog;

public class ScratchActivity extends BaseActivity {

    private Context mContext;
    private Activity mActivity;

//    @BindView(R.id.sign)
    CJNaviBar naviBar;
//    @BindView(R.id.scratch_scratchView)
    ScratchView scratchImageView;
//    @BindView(R.id.scatch_text)
    TextView tx_scrachText;
//    @BindView(R.id.scratch_background_layout)
    RelativeLayout backgroundLayout;
//    @BindView(R.id.scrath_ad_frameLayout)
    FrameLayout ad_layout;

    private CJSignDialog coinDialog;
    private QMUITipDialog waitDialog;
    private float maxNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scratch);
        mContext = this;
        mActivity = this;

        naviBar = (CJNaviBar) findViewById(R.id.scratch_naviBar);
        scratchImageView = (ScratchView)findViewById(R.id.scratch_scratchView);
        tx_scrachText = (TextView)findViewById(R.id.scatch_text);
        backgroundLayout = (RelativeLayout)findViewById(R.id.scratch_background_layout);
        ad_layout = (FrameLayout)findViewById(R.id.scrath_ad_frameLayout);

        CJHandle.getInstance().register(new CJHandleObject(CJHanlderType.GGK, myHandler));

        init();
        setBackgound();
        if(isShowVideoAd()){
            CJAdCenter.getInstance().showRewardVidewo(CJAdCenter.AD_Place_ggk_video,mActivity, mContext,null);
        }
        loadData();
    }

    private void setBackgound(){
        naviBar.getBackgroundLayout().setBackground(null);
        naviBar.getTitle().setTextColor(getResources().getColor(R.color.white));
        Bitmap bitmap   = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.card_bg_2);
        bitmap = V2BlurUtils.blurBitmap(this, bitmap, 16);
        bitmap = V2BlurUtils.cropBitmap(bitmap, QMUIDisplayHelper.getScreenWidth(mContext), QMUIDisplayHelper.getScreenHeight(mContext));
        backgroundLayout.setBackground(new BitmapDrawable(getResources(),bitmap));
    }

    private void init() {
        tx_scrachText.setText("金币 +0");

        scratchImageView.setOnCompleteListener(() -> {
            //大奖刮完
            CJLogger.v("scratchImageView setOnCompleteListener");
            waitDialog.show();
            new Thread(() -> {
                //上报
                boolean ret = CJZManager.getInstance().ggkReport(CJGGKHelper.getInstance().getCard_sn());
                if (!ret)
                    ret = CJZManager.getInstance().ggkReport(CJGGKHelper.getInstance().getCard_sn());
                if (ret) {
                    addCount();
                    CJGGKHelper.getInstance().addUserCoinOrCash();
                    CJGGKHelper.getInstance().changeStatusToUsed(CJGGKHelper.getInstance().getCard_sn());
//                    CJEvent.Scratch.getEventtBus().post(new CJEventObject(CJEventObjectType.ShowDialog, null));
                    myHandler.sendEmptyMessage(ShowDialog);
                    runOnUiThread(() ->{
                        scratchImageView.setVisibility(View.GONE);
                    });
                }
                else{
                    runOnUiThread(() ->{
                        CJCore.Toast(mContext,"网络错误",false);
                        back();
                    });
                }
                waitDialog.dismiss();
            }).start();
            scratchImageView.setOnCompleteListener(null);
        });

        naviBar.setLeftBtnClickListener(v -> back());

        waitDialog = CJDialog.getTipDialog(CJDialog.CJDialog_TIP_TYPE_LOADING, mContext, "正在加载");

        CJAdCenter.getInstance().showNavi(CJAdCenter.AD_Place_ggk_navi,mContext,
                QMUIDisplayHelper.getScreenWidth(mContext),
                CJDpSpDxTransform.px2dip(ad_layout.getLayoutParams().height),
                ad_layout,mActivity,
                null);
    }

    private void showCoinDialog() {
        CJLogger.v("showCoinDialog cardType:" + CJGGKHelper.getInstance().getCardType());
        coinDialog = new CJSignDialog(mActivity,mContext, CJGGKHelper.getInstance().getCardAward_str_number());
        coinDialog.setBtnText("好的");
        coinDialog.hideAppLayoutChilds();
        if(CJGGKHelper.getInstance().getCardType() == CJGGKHelper.CARD_TYPE_COIN){
            coinDialog.setTypeIcon(R.drawable.task_coin);
        }
        else if(CJGGKHelper.getInstance().getCardType() == CJGGKHelper.CARD_TYPE_CASH){
            coinDialog.setTypeIcon(R.drawable.money_dialog);
        }
        else if(CJGGKHelper.getInstance().getCardType() == CJGGKHelper.CARD_TYPE_CANCASH){
            coinDialog.setTypeIcon(R.drawable.card_logo);
        }
        coinDialog.setBtnOnClickListerner(v -> {
            coinDialog.dismiss();
            back();
        });
        coinDialog.setCloseBtnOnClickListerner(v -> {
            coinDialog.dismiss();
            back();
        });
        coinDialog.show();
        CJGGKHelper.getInstance().removeWatchCard();
        CJAdCenter.getInstance().showNavi_dialog(CJAdCenter.AD_Place_ggk_Dialoig_navi,mContext, coinDialog.getWidth(), coinDialog.getHieght(), coinDialog.getAppLayout(), mActivity, type -> {
            CJLogger.v("Scratch dialog type:" + type);
            switch (type) {
                case CJAdCenter.AD_Status_Close: {
                }
                break;
                case CJAdCenter.AD_Status_HideDialog: {
                    coinDialog.hideDialog();
                }
                break;
                case CJAdCenter.AD_Status_ShowDialog: {
                    coinDialog.show();
                }
                break;
                case CJAdCenter.AD_Status_OpenFrame: {
                    coinDialog.openAdFrame();
                }
                break;
                case CJAdCenter.AD_Status_CloseFrame: {
                    coinDialog.closeAdFrame();
                }
                break;
                default:
                    break;
            }
        });
//        CJCSJ_Navi.getInstance().getNativeAd(mContext, coinDialog.getWidth(), 0, coinDialog.getAppLayout(), (String str) -> {
//            //下载完成
//            CJLogger.v("download finish");
//        }, mActivity,null);
    }

    private void showData() {
        if(CJGGKHelper.getInstance().getCardAward().length() > 0){
            tx_scrachText.setText(CJGGKHelper.getInstance().getCardAward());
        }
    }

    private void loadData() {
        waitDialog.show();
        new Thread(()->{
            boolean ret = CJZManager.getInstance().ggkDetail(CJGGKHelper.getInstance().getCard_sn());
            if (ret) {
                waitDialog.dismiss();
//                CJEvent.Scratch.getEventtBus().post(new CJEventObject(CJEventObjectType.LoadFinish, null));
                myHandler.sendEmptyMessage(LoadFinish);
            } else {
                waitDialog.dismiss();
                CJCore.ToastInMainThread(mActivity,mContext, "网络错误，请重试", false);
                back();
            }
        }).start();
    }

    private void addCount(){
        int count   = CJStorage.getInstance().getIntData(mContext,CJStorage.SCRATCH_MapName,CJStorage.SCRATCH_KEY_COUNT);
        count++;
        if(count == CJGGKHelper.getInstance().ScratchAdTime + 1){ //3次刮卡 弹出一次广告
            count = 0;
        }
        CJStorage.getInstance().pushIntData(mContext,CJStorage.SCRATCH_MapName,CJStorage.SCRATCH_KEY_COUNT,count);
    }

    private boolean isShowVideoAd(){
        int count   = CJStorage.getInstance().getIntData(mContext,CJStorage.SCRATCH_MapName,CJStorage.SCRATCH_KEY_COUNT);
        CJLogger.v("isShowVideoAd count:"+ count);
        if(count == CJGGKHelper.getInstance().ScratchAdTime){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public void getMessage(Message msg) {
        switch (msg.what) {
            case LoadFinish: {
                CJLogger.v("LoadFinish");
                waitDialog.dismiss();
                showData();
            }
            break;
            case ShowDialog: {
                CJLogger.v("getEventBusMessage ShowDialog");
                showCoinDialog();
            }
            break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        CJLogger.v("scratch onDestroy");
        CJCSJ_Navi.getInstance().releaseAd();
        CJHandle.getInstance().unRegister(CJHanlderType.GGK);
        super.onDestroy();
    }

}
